const vm = new Vue({
    el: '#app',
    data: {
        isDisabled: false,
        codeText: '获取验证码',
        loginForm: {
            plateNo: '',
            mobile: '',
            smsCode: ''
        },
    },
    methods: {

        getCode() {
            this.$indicator.open();
            if (this.loginForm.plateNo == undefined || this.loginForm.plateNo == ''
                || this.loginForm.mobile == undefined || this.loginForm.mobile == '') {
                this.$toast('车牌号和手机号不能为空')
                return
            }
            if (this.loginForm.mobile.length != 11) {
                this.$toast('手机号格式不正确')
                return
            }

            let time = 60;
            let i = 0;
            let that = this;
            //1.在60秒之内，button上倒计时60秒，并显示
            //2.向controller请求验证码，并发送到指定的手机号上面
            let set = setInterval(function () {
                that.codeText = (--time) + "秒后重发";
                i++;
                //此变量用于标识只调用一次获取验证码的函数
                if (i == 1) {
                    console.log("get code start1 ... ");
                    //使获取验证码按钮不可点击
                    that.isDisabled = true
                    that.getVerificationCode();
                }
            }, 1000);

            //60秒之后需要做的事情
            setTimeout(function () {
                //恢复获取验证码按钮可点击
                that.isdisabled = false
                that.codeText = "获取验证码";
                clearInterval(set);//清除计时器
            }, time * 1000); //60000
        },
        getVerificationCode() {
            axios.get(basePath + 'sms/code/' + this.loginForm.mobile)
                .then(resp => {
                    this.$indicator.close();
                    if (resp.data.code == 0) {
                        this.$toast('验证码发送成功')
                    } else{
                        this.$toast({
                            message: '验证码发送失败',
                            iconClass: 'icon icon-error'
                        })
                    }
                })
        },
        submit() {
            if (!this.loginForm.plateNo) {
                this.$toast('车牌号为空')
                return;
            }
            if (!this.loginForm.mobile) {
                this.$toast('手机号为空')
                return;
            }
            if (!this.loginForm.smsCode) {
                this.$toast('验证码为空')
                return;
            }

            this.$indicator.open({
                text: '提交中...',
                spinnerType: 'fading-circle'
            });
            axios.post(basePath + 'sms/login', this.loginForm)
                .then(resp => {
                    this.$indicator.close();
                    if (resp.data.code === 0) {
                        window.location = basePath + "index.html";
                    } else {
                        this.$messagebox('提示', resp.data.msg);
                    }
                })
        }
    },
    computed: {
        // 给确定按钮添加样式
        confirmBtnClass() {
            return this.loginForm.plateNo && this.loginForm.mobile && this.loginForm.smsCode
        }
    },
    created() {
    }
})